package com.sh.ndt.common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.SavepointManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public abstract class BaseDAOImpl extends JdbcDaoSupport implements RowMapper {

	private static final Logger LOGGER = Logger.getLogger(BaseDAOImpl.class);

	protected PlatformTransactionManager transactionManager;
	protected TransactionStatus status;
	protected SavepointManager savePointManager;

	public void setTransactionManager(PlatformTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}

	public void beginTransaction() throws Exception {
		LOGGER.debug("Transaction Initiated  ::: ");

		TransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
		this.status = transactionManager.getTransaction(transactionDefinition);

	}

	public void endTransaction(TransactionStatus status) throws Exception {

		LOGGER.debug("Transaction Comitted  ::: " + status);
		transactionManager.commit(status);

	}

	public void rollBackTransaction(TransactionStatus status) throws Exception {
		LOGGER.debug("Rolling Back Transaction Status ::: " + status);
		transactionManager.rollback(status);
	}

	public abstract Object mapRow(ResultSet arg0, int arg1) throws SQLException;
}
